home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / prgtools / euphor13.zip / REFMAN.DOC < prev    next >
Text File  |  1995-05-29  |  55KB  |  1,326 lines

  1.  
  2.         Euphoria Programming Language 
  3.             version 1.3
  4.               Reference Manual
  5.  
  6.  
  7.         (c) 1995 Rapid Deployment Software
  8.     
  9.         Permission is freely granted to anyone
  10.         to copy this manual.
  11.  
  12.  
  13.  
  14.  
  15.             TABLE OF CONTENTS
  16.             =================
  17.  
  18.      Part I - Core Language - refman.doc
  19.  
  20.     1. Introduction 
  21.       
  22.        1.1  Example Program                  
  23.        1.2  Installation             
  24.        1.3  Running a Program    
  25.        1.4  Editing a Program   
  26.        1.5  Distributing a Program
  27.     
  28.     2. Language Definition        
  29.      
  30.        2.1  Objects                 
  31.        2.2  Expressions     
  32.        2.3  Declarations 
  33.        2.4  Statements      
  34.        2.5  Top-Level Commands 
  35.     
  36.     3. Debugging 
  37.  
  38.  
  39.      Part II - Library Routines - see library.doc
  40.     
  41.     1. Introduction
  42.             
  43.     2. Routines by Application Area
  44.     
  45.     3. Alphabetical Listing of all Routines
  46.       
  47.  
  48.  
  49.  
  50.     
  51.             1. Introduction
  52.             ===============
  53.         
  54.  Euphoria is a new programming language with the following advantages over 
  55.  conventional languages:
  56.  
  57.  o      a remarkably simple, flexible, powerful language
  58.     definition that is extremely easy to learn and use.  
  59.  
  60.  o      dynamic storage allocation. Variables grow or shrink
  61.     without the programmer having to worry about allocating
  62.     and freeing chunks of memory.  Objects of any size can be 
  63.     assigned to an element of a Euphoria sequence (array).
  64.  
  65.  o      a high-performance, state-of-the-art interpreter that is
  66.     10 to 20 times faster than conventional interpreters such as
  67.     Microsoft QBasic.  
  68.  
  69.  o      lightning-fast pre-compilation. Your program is checked
  70.     for syntax and converted into an efficient internal form at
  71.     over 12,000 lines per second on a 486-50. 
  72.  
  73.  o      extensive run-time checking for: out-of-bounds subscripts,
  74.     uninitialized variables, bad parameter values for built-in
  75.     functions, illegal value assigned to a variable and many 
  76.     more.  There are no mysterious machine exceptions -- you 
  77.     will always get a full English description of any problem 
  78.     that occurs with your program at run-time, along with a 
  79.     call-stack trace-back and a dump of all of your variable
  80.     values.  Programs can be debugged quickly, easily and
  81.     more thoroughly.
  82.  
  83.  o      features of the underlying hardware are completely hidden. 
  84.     Programs are not aware of word-lengths,    underlying bit-level 
  85.     representation of values, byte-order etc. Euphoria programs are 
  86.     therefore highly portable from one machine to another.
  87.  
  88.  o      a full-screen source debugger and an execution profiler
  89.     are included, along with a full-screen, multi-file editor. 
  90.     On a color monitor, the editor displays Euphoria programs in 
  91.     multiple colors, to highlight comments, reserved words, 
  92.     built-in functions, strings, and level of nesting of brackets. 
  93.     It optionally performs auto-completion of statements, 
  94.     saving you typing effort and reducing syntax errors. This 
  95.     editor is written in Euphoria, and the source code is 
  96.     provided to you without restrictions. You are free to
  97.     modify it, add features, and redistribute it as you wish. 
  98.  
  99.  o      Euphoria programs run under MS-DOS (or Windows or OS/2), but 
  100.      are not    subject to any 64K or 640K memory limitations. You can
  101.     create programs that use the full multi-megabyte memory
  102.     of your computer. You can even set up a swap file for
  103.     programs that need more memory than exists on your machine. 
  104.  
  105.  o      Euphoria routines are naturally generic. The example
  106.     program below shows a single routine that will sort any
  107.     type of data -- integers, floating-point numbers, strings
  108.     etc. Euphoria is not an "Object-Oriented" language in the 
  109.     usual sense, yet it achieves many of the benefits of these
  110.     languages in a much simpler way. 
  111.  
  112.  
  113.  1.1 Example Program 
  114.  -------------------
  115.  
  116.  The following is an example of a complete Euphoria program.
  117.  
  118.  
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120.  
  121.  sequence list, sorted_list
  122.  
  123.  function merge_sort(sequence x)
  124.  -- put x into ascending order using a recursive merge sort
  125.      integer n, mid
  126.      sequence merged, a, b
  127.  
  128.      n = length(x)
  129.      if n = 0 or n = 1 then
  130.      return x  -- trivial case
  131.      end if
  132.  
  133.      mid = floor(n/2)
  134.      a = merge_sort(x[1..mid])       -- sort first half of x 
  135.      b = merge_sort(x[mid+1..n])     -- sort second half of x
  136.  
  137.      -- merge the two sorted halves into one
  138.      merged = {}
  139.      while length(a) > 0 and length(b) > 0 do
  140.      if compare(a[1], b[1]) < 0 then
  141.          merged = append(merged, a[1])
  142.          a = a[2..length(a)]
  143.      else
  144.          merged = append(merged, b[1])
  145.          b = b[2..length(b)]
  146.      end if
  147.      end while
  148.      return merged & a & b  -- merged data plus leftovers
  149.  end function
  150.  
  151.  procedure print_sorted_list()
  152.  -- generate sorted_list from list 
  153.      list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
  154.      sorted_list = merge_sort(list)
  155.      ? sorted_list   
  156.  end procedure
  157.  
  158.  print_sorted_list()     -- this command starts the program 
  159.  
  160.  
  161. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162.  
  163.  
  164.  The above example contains 4 separate commands that are processed in order.
  165.  The first declares two variables: list and sorted_list to be sequences. 
  166.  The second defines a function merge_sort(). The third defines a procedure 
  167.  print_sorted_list(). The final command calls procedure print_sorted_list().
  168.  
  169.  The output from the program will be:
  170.   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. 
  171.  
  172.  merge_sort() will just as easily sort {1.5, -9, 1e6, 100} or
  173.  {"oranges", "apples", "bananas"} .
  174.  
  175.  This example is stored as euphoria\demo\example.ex. This is not the fastest 
  176.  way to sort in Euphoria. Go to the euphoria\demo directory and type 
  177.  "ex allsorts" to see timings on several different sorting algorithms for 
  178.  increasing numbers of objects. For a quick tutorial example of Euphoria 
  179.  programming see euphoria\demo\bench\filesort.ex.
  180.  
  181.  
  182.  1.2 Installation 
  183.  ----------------
  184.  
  185.  To install Euphoria on your machine, first read the file install.doc. 
  186.  Installation simply involves copying the euphoria files to your hard disk 
  187.  under a directory named "EUPHORIA", and then modifying your autoexec.bat file
  188.  so that EUPHORIA\BIN is on your search path, and the environment variable 
  189.  EUDIR is set to the EUPHORIA directory. An automatic install program, 
  190.  "install.bat" is provided for this purpose. For the latest details, please 
  191.  read the instructions in install.doc before you run install.bat.
  192.  
  193.  When installed, the euphoria directory will look something like this: 
  194.  
  195.     euphoria 
  196.         readme.doc
  197.         \bin        
  198.             ex.exe, dos4gw.exe, ed.bat, other utilities
  199.         \include
  200.             standard include files, e.g. graphics.e
  201.         \doc
  202.             ed.doc, refman.doc etc.
  203.         \demo   
  204.             demo programs, e.g. ttt.ex, mset.ex, plot3d.ex
  205.  
  206.             \langwar
  207.                language war game, lw.ex
  208.             \bench  
  209.                benchmark programs
  210.            
  211.  
  212.  1.3 Running a Program
  213.  ------------------------
  214.  
  215.  Euphoria programs are executed by typing "ex", followed by the name of the 
  216.  main (or only) file. By convention, main Euphoria files have an extension of 
  217.  ".ex".  Other Euphoria files, that are meant to be included in a larger
  218.  program, end in ".e". To save typing, you can leave off the ".ex", and 
  219.  the ex command will supply it for you automatically. If the file can't be 
  220.  found in the current directory, your PATH will be searched. There are no 
  221.  command-line options for ex itself, but your program can call the built-in 
  222.  function command_line() to read the ex command-line. You can redirect 
  223.  standard input and standard output when you run a Euphoria program, 
  224.  for example:
  225.  
  226.     ex filesort.ex < raw > sorted
  227.  
  228.        or simply,
  229.  
  230.     ex filesort < raw > sorted
  231.  
  232.  For frequently-used programs you might want to make a small .bat file 
  233.  containing something like:
  234.      
  235.       @echo off
  236.       ex myprog.ex %1 %2
  237.  
  238.  where myprog.ex expects two command-line arguments. This will save you 
  239.  from typing ex all the time. 
  240.  
  241.  ex.exe is in the euphoria\bin directory which must be on your search path. 
  242.  The file dos4gw.exe must also be present in the bin directory (or somewhere 
  243.  on the search path). Some Euphoria programs expect the environment variable 
  244.  EUDIR to be set to the main Euphoria directory.
  245.  
  246.  Running Under Windows
  247.  ---------------------
  248.  You can run Euphoria programs directly from the Windows environment, or from 
  249.  a DOS shell that you have opened from Windows. By "associating" .ex files
  250.  with ex.exe, you can simply double-click on a .ex file to run it. It
  251.  is possible to have several Euphoria programs active in different windows.
  252.  You can resize these windows, move them around, change to a different font, 
  253.  run things in the background, copy and paste between windows etc. See your 
  254.  Windows manual. The Euphoria editor is available. You might want to 
  255.  associate .e, .pro and other text files with ed.bat. Also, the File-menu/
  256.  Run-command will let you type in ex or ed command lines. 
  257.  
  258.  Use of a swap file
  259.  ------------------
  260.  If you run a Euphoria program under Windows (or in a DOS shell under
  261.  Windows) and the program runs out of physical memory, it will start
  262.  using "virtual memory". Windows provides this virtual memory by swapping
  263.  out least-recently-used data to a swap file. To change the size of the 
  264.  Windows swap file, click on Control Panel / 386 Enhanced / 
  265.  "virtual memory...".
  266.  
  267.  Under DOS, outside of Windows, there is normally no swap file. However
  268.  by typing:  swapon  you can tell the DOS4GW DOS-extender to create a 
  269.  temporary 16-megabyte swap file for use by Euphoria programs. This file is
  270.  created when each Euphoria program starts, and is deleted when the program
  271.  terminates. Type:  swapoff  to turn off this swapping feature. Do not enable
  272.  swapping unless your program needs it, as it adds a bit of overhead to the
  273.  startup and termination of each Euphoria program.
  274.  
  275.  When disk swapping activity occurs your program will run correctly 
  276.  but will slow down. A better approach may be to free up more extended 
  277.  memory by cutting back on SMARTDRV and other programs that reserve large 
  278.  amounts of extended memory for themselves.
  279.  
  280.  
  281.  1.4 Editing a Program 
  282.  ---------------------
  283.  
  284.  You can use any text editor to edit a Euphoria program. However, Euphoria 
  285.  comes with its own special editor that is written entirely in Euphoria. 
  286.  Type: ed  followed by the complete name of the file you wish to edit (the 
  287.  .ex extension is not assumed). You can use this editor to edit any kind of 
  288.  text file. When you edit a .e or .ex file some extra features, such as color
  289.  syntax highlighting and auto-completion of certain statements, are available
  290.  to make your job easier. 
  291.  
  292.  Whenever you run a Euphoria program and get an error message, during 
  293.  compilation or execution, you can simply type ed with no file name and you 
  294.  will be automatically positioned in the file containing the error, at 
  295.  the correct line and column, and with the error message displayed at the 
  296.  top of the screen. 
  297.  
  298.  Under Windows you can associate ed.bat with various kinds of text files 
  299.  that you want to edit.
  300.  
  301.  Most keys that you type are inserted into the file at the cursor position. 
  302.  Hit the Esc key once to get a menu bar of special commands. The arrow keys, 
  303.  and the Insert/Delete Home/End PageUp/PageDown keys are also active. See 
  304.  the file euphoria\doc\ed.doc for a complete description of the editing 
  305.  commands. Esc h (help) will let you view ed.doc from your editing session.
  306.  
  307.  If you need to understand or modify any detail of the editor's operation, 
  308.  you can edit the file ed.ex in euphoria\bin (be sure to make a backup 
  309.  copy so you don't lose your ability to edit).  If the name ed conflicts 
  310.  with some other command on your system, simply rename the file 
  311.  euphoria\bin\ed.bat to something else.  Because this editor is written 
  312.  in Euphoria, it is remarkably concise and easy to understand. The same 
  313.  functionality implemented in a language like C, would take far more 
  314.  lines of code.
  315.  
  316.  
  317.  1.5 Distributing a Program
  318.  --------------------------
  319.  
  320.  Your customer needs to have the 2 files: ex.exe and dos4gw.exe somewhere 
  321.  on the search path. You are free to supply anyone with the Public Domain
  322.  Edition of ex.exe, as well as dos4gw.exe to support it.
  323.  
  324.  Your program can be distributed in source form or in shrouded form. In source
  325.  form you supply your Euphoria files plus any standard include files that are
  326.  required. To deliver a program in shrouded form, you run the Euphoria source 
  327.  code shrouder, bin\shroud.ex, against your main Euphoria file. The shrouder 
  328.  pulls together all included files into a single compact file that is 
  329.  virtually unreadable. You then ship this one file plus a copy of ex.exe and 
  330.  dos4gw.exe. One copy of ex.exe and dos4gw.exe on a machine is sufficient to 
  331.  run any number of Euphoria programs. Comments in bin\shroud.ex tell you how 
  332.  to run it, and what it does to obscure or "shroud" your source. 
  333.  
  334.  
  335.  
  336.             2. Language Definition
  337.             ======================
  338.  
  339.  2.1 Objects
  340.  -----------
  341.  
  342.  All data objects in Euphoria are either atoms or sequences.  An atom is a 
  343.  single numeric value. A sequence is an ordered list of data objects.  
  344.  The objects contained in a sequence can be an arbitrary mix of atoms or 
  345.  sequences. A sequence is represented by a list of objects in brace brackets,
  346.  separated by commas. Atoms can have any integer or double-precision floating 
  347.  point value. They can range from approximately -1e300 to +1e300 with 15 
  348.  decimal digits of accuracy. Here are some Euphoria objects:
  349.  
  350.     -- examples of atoms:
  351.     0
  352.     1000
  353.     98.6
  354.     -1e6
  355.  
  356.     -- examples of sequences:
  357.     {2, 3, 5, 7, 11, 13, 17, 19}        -- 8-element sequence
  358.     {1, 2, {3, 3, 3}, 4, {5, {6}}}      -- 5-element sequence
  359.     {{"jon", "smith"}, 52389, 97.25}    -- 3-element sequence
  360.     {}                                  -- 0-element sequence
  361.  
  362.  Numbers can also be entered in hexadecimal. For example:
  363.     #FE             -- 254
  364.     #A000           -- 40960
  365.     #FFFF00008      -- 68718428168
  366.     -#10            -- -16
  367.  
  368.  Sequences can be nested to any depth.  Brace brackets are used to construct 
  369.  sequences out of a list of expressions.  These expressions are evaluated at 
  370.  run-time. e.g.
  371.  
  372.     {x+6, 9, y*w+2, sin(0.5)}
  373.  
  374.  The "Hierarchical Objects" part of the Euphoria acronym comes from the
  375.  hierarchical nature of nested sequences. This should not be confused with
  376.  the class hierarchies of certain object-oriented languages. 
  377.  
  378.  Performance Note: The Euphoria interpreter will store integer-valued atoms as
  379.  machine integers to save time and space. 
  380.  
  381.  
  382.  Character Strings
  383.  -----------------
  384.  Character strings may be entered using quotes e.g.
  385.  
  386.     "ABCDEFG"
  387.  
  388.  Strings are just sequences of characters, and may be manipulated and 
  389.  operated upon just like any other sequences. For example the above 
  390.  string is equivalent to the sequence
  391.  
  392.     {65, 66, 67, 68, 69, 70, 71}
  393.  
  394.  which contains the corresponding ASCII codes. Similarly, "" is
  395.  equivalent to {}. Both represent the sequence of length-0. As a 
  396.  matter of programming style, it is natural to use "" to suggest 
  397.  a length-0 sequence of characters, and {} to suggest some other 
  398.  kind of sequence.
  399.  
  400.  Individual characters may be entered using single quotes if it is 
  401.  desired that they be treated as individual numbers (atoms) and not
  402.  length-1 sequences. e.g.
  403.  
  404.      'B'   -- equivalent to the atom 66
  405.      "B"   -- equivalent to the sequence {66}
  406.  
  407.  Note that an atom is not equivalent to a one-element sequence containing 
  408.  the same value, although there are a few built-in routines that choose 
  409.  to treat them similarly.
  410.  
  411.  Special characters may be entered using a back-slash:
  412.  
  413.     \n        newline
  414.     \r        carriage return
  415.     \t        tab
  416.     \\        backslash
  417.     \"        double quote
  418.     \'        single quote
  419.  
  420.  For example, "Hello, World!\n", or '\\'. The Euphoria editor displays 
  421.  character strings in brown. 
  422.  
  423.  Comments
  424.  --------
  425.  Comments are started by two dashes and extend to the end of the current line.
  426.  e.g.
  427.  
  428.      -- this is a comment
  429.  
  430.  Comments are ignored by the compiler and have no effect on execution speed. 
  431.  The editor displays comments in red. In this manual we use italics.
  432.  
  433.  
  434.  2.2 Expressions
  435.  ---------------
  436.  
  437.  Objects can be combined into expressions using binary and unary operators as 
  438.  well as built-in and user-defined functions. For example,
  439.  
  440.     {1,2,3} + 5
  441.  
  442.  is an expression that adds the sequence {1,2,3} and the atom 5 to get the 
  443.  resulting sequence {6,7,8}. Besides + there are many other operators. The 
  444.  precedence of operators is as follows:
  445.  
  446.     highest precedence:     function/type calls
  447.  
  448.                 unary-  unary+  not 
  449.  
  450.                 *  /
  451.  
  452.                 +  -
  453.  
  454.                 &
  455.  
  456.                 <  >  <=  >=  =  !=
  457.  
  458.      lowest precedence:     and, or
  459.  
  460.  Thus 2+6*3 means 2+(6*3), not (2+6)*3. Operators on the same line above have 
  461.  equal precedence and are evaluated left to right.
  462.  
  463.  
  464.  Relational & Logical Operators
  465.  ------------------------------
  466.  The relational operators, <, >, <=, >=, = , != each produce a 1 (true) or a 
  467.  0 (false) result. These results can be used by the logical operators 'and', 
  468.  'or', and 'not' to determine an overall truth value. e.g.
  469.  
  470.     b > 0 and b != 100 or not (c <= 5) 
  471.  
  472.  where b and c are the names of variables.  
  473.  
  474.  
  475.  Subscripting of Sequences
  476.  -------------------------
  477.  A single element of a sequence may be selected by giving the element number 
  478.  in square brackets. Element numbers start at 1. Non-integer subscripts are 
  479.  rounded down to an integer. 
  480.  
  481.  For example, if x contains {5, 7, 9, 11, 13} then x[2] is 7. Suppose we 
  482.  assign something different to x[2]:
  483.  
  484.     x[2] = {11,22,33}
  485.  
  486.  Then x becomes: {5, {11,22,33}, 9, 11, 13}. Now if we ask for x[2] we get 
  487.  {11,22,33} and if we ask for x[2][3] we get the atom 33. If you try to 
  488.  subscript with a number that is outside of the range 1 to the number of 
  489.  elements, you will get a subscript error. For example x[0],  x[-99] or 
  490.  x[6] will cause errors. So will x[1][3] since x[1] is not a sequence. There 
  491.  is no limit to the number of subscripts that may follow a variable, but 
  492.  the variable must contain sequences that are nested deeply enough. The 
  493.  two dimensional array, common in other languages, can be easily simulated 
  494.  with a sequence of sequences:
  495.  
  496.      { {5, 6, 7, 8, 9},
  497.        {1, 2, 3, 4, 5},
  498.        {0, 1, 0, 1, 0} }
  499.  
  500.  An expression of the form x[i][j] can be used to access any element. The two 
  501.  dimensions are not symmetric however, since an entire "row" can be selected 
  502.  with x[i], but there is no simple expression to select an entire column. 
  503.  Other logical structures, such as n-dimensional arrays, arrays of strings, 
  504.  arrays of structures etc. can also be handled easily and flexibly.
  505.     
  506.  Note that expressions in general may not be subscripted, just variables. For 
  507.  example: {5,6,7,8}[3] is not supported. 
  508.  
  509.  
  510.  Slicing of Sequences
  511.  --------------------
  512.  A sequence of consecutive elements may be selected by giving the starting and 
  513.  ending element numbers. For example if x is {1, 1, 2, 2, 2, 1, 1, 1} then 
  514.  x[3..5] is the sequence {2, 2, 2}. x[3..3] is the sequence {2}. x[3..2] is 
  515.  also allowed. It evaluates to the length-0 sequence {}.  If y has the value: 
  516.  {"fred", "george", "mary"} then y[1..2] is {"fred", "george"}.
  517.  
  518.  We can also use slices for overwriting portions of variables. After x[3..5] = 
  519.  {9, 9, 9} x would be {1, 1, 9, 9, 9, 1, 1, 1}. We could also have said 
  520.  x[3..5] = 9 with the same effect. Suppose y is {0, "Euphoria", 1, 1}. 
  521.  Then y[2][1..4] is "Euph". If we say y[2][1..4]="ABCD" then y will 
  522.  become {0, "ABCDoria", 1, 1}.
  523.  
  524.  We need to be a bit more precise in defining the rules for empty slices. 
  525.  Consider a slice s[i..j] where s is of length n. A slice from i to j, 
  526.  where  j = i-1  and i >= 1 produces the empty sequence, even if i = n+1. 
  527.  Thus 1..0  and n+1..n and everything in between are legal (empty) slices. 
  528.  Empty slices are quite useful in many algorithms. A slice from i to j where 
  529.  j < i - 1 is illegal , i.e. "reverse" slices such as s[5..3] are not allowed. 
  530.  
  531.  
  532.  Concatenation of Sequences and Atoms
  533.  ------------------------------------
  534.  Any two objects may be concatenated using the & operator. The result is a 
  535.  sequence with a length equal to the sum of the lengths of the concatenated 
  536.  objects (where atoms are considered here to have length 1). e.g.
  537.  
  538.     {1, 2, 3} & 4   -- result is {1, 2, 3, 4}
  539.  
  540.     4 & 5           -- result is {4, 5}
  541.  
  542.     {{1, 1}, 2, 3} & {4, 5}   -- result is {{1, 1}, 2, 3, 4, 5}
  543.  
  544.     x = {}  
  545.     y = {1, 2}
  546.     y = y & x       -- y is still {1, 2}
  547.  
  548.  
  549.  Arithmetic Operations on Sequences
  550.  ----------------------------------
  551.  Any binary or unary arithmetic operation, including any of the built-in 
  552.  math routines, may be applied to entire sequences as well as to single 
  553.  numbers.
  554.  
  555.  When applied to a sequence, a unary operator is actually applied to each 
  556.  element in the sequence to yield a sequence of results of the same length. 
  557.  If one of these elements is itself a sequence then the same rule is applied 
  558.  recursively. e.g.
  559.  
  560.     x = -{1, 2, 3, {4, 5}}   -- x is {-1, -2, -3, {-4, -5}}
  561.  
  562.  If a binary operator has operands which are both sequences then the two 
  563.  sequences must be of the same length. The binary operation is then applied 
  564.  to corresponding elements taken from the two sequences to get a sequence of 
  565.  results. e.g.
  566.  
  567.     x = {5, 6, 7 {1, 1}} + {10, 10, 20, 100}
  568.     -- x is {15, 16, 27, {101, 101}}
  569.  
  570.  If a binary operator has one operand which is a sequence while the other is a 
  571.  single number (atom) then the single number is effectively repeated to 
  572.  form a sequence of equal length to the sequence operand. The rules for 
  573.  operating on two sequences then apply. Some examples:
  574.  
  575.     y = {4, 5, 6}
  576.  
  577.     w = 5 * y  -- w is {20, 25, 30}
  578.  
  579.     x = {1, 2, 3}
  580.     
  581.     z = x + y  -- z is {5, 7, 9}
  582.  
  583.     z = x < y  -- z is {1, 1, 1}
  584.  
  585.     w = {{1, 2}, {3, 4}, {5}}
  586.    
  587.     w = w * y  -- w is {{4, 8}, {15, 20}, {30}}
  588.  
  589.  
  590.  Comparison of Euphoria Objects with Other Languages
  591.  ---------------------------------------------------
  592.  By basing Euphoria on this one, simple, general, recursive data structure, 
  593.  a tremendous amount of the complexity normally found in programming languages
  594.  has been avoided. The arrays, record structures, unions, arrays of records, 
  595.  multidimensional arrays, etc. of other languages can all be easily 
  596.  simulated in Euphoria with sequences. So can higher-level structures such
  597.  as lists, stacks, queues, trees etc. 
  598.  
  599.  Furthermore, in Euphoria you can have sequences of mixed type; you can 
  600.  assign any object to an element of a sequence; and sequences easily grow or 
  601.  shrink in length without your having to worry about storage allocation issues.
  602.  The exact layout of a data structure does not have to be declared in advance,
  603.  and can change dynamically as required. It is easy to write generic code,
  604.  where, for instance, you push or pop a mix of various kinds of data 
  605.  objects using a single stack. 
  606.  
  607.  Data structure manipulations are very efficient since Euphoria will point to 
  608.  large data objects rather than copy them. 
  609.  
  610.  Programming in Euphoria is based entirely on creating and manipulating 
  611.  flexible, dynamic sequences of data. Sequences are it - there are no
  612.  other data structures to learn. You operate in a simple, safe, elastic world 
  613.  of *values*, that is far removed from the rigid, tedious, dangerous world
  614.  of bits, bytes, pointers and machine crashes. 
  615.  
  616.  Unlike other languages such as LISP and Smalltalk, Euphoria's 
  617.  "garbage collection" of unused storage is a continuous process that never 
  618.  causes random delays in execution of a program, and does not pre-allocate 
  619.  huge regions of memory.   
  620.  
  621.  The language definitions of conventional languages such as C, C++, Ada, etc.
  622.  are very complex. Most programmers become fluent in only a subset of the 
  623.  language. The ANSI standards for these languages read like complex legal 
  624.  documents. 
  625.  
  626.  You are forced to write different code for different data types simply to 
  627.  copy the data, ask for its current length, concatenate it, compare it etc. 
  628.  The manuals for those languages are packed with routines such as "strcpy",
  629.  "strncpy", "memcpy", "strcat",  "strlen", "strcmp", "memcmp", etc. that 
  630.  each only work on one of the many types of data.
  631.  
  632.  Much of the complexity surrounds issues of data type. How do you define 
  633.  new types? Which types of data can be mixed? How do you convert one type 
  634.  into another in a way that will keep the compiler happy? When you need to 
  635.  do something requiring flexibility at runtime, you frequently find yourself 
  636.  trying to fake out the compiler.
  637.  
  638.  In these languages the numeric value 4 (for example) can have a different 
  639.  meaning depending on whether it is an int, a char, a short, a double, an  
  640.  int * etc.. In Euphoria, 4 is the atom 4, period. Euphoria has something 
  641.  called types as we shall see later, but it is a much simpler concept.
  642.  
  643.  Issues of dynamic storage allocation and deallocation consume a great deal 
  644.  of programmer coding time and debugging time in these other languages, and 
  645.  make the resulting programs much harder to understand. 
  646.  
  647.  Pointer variables are extensively used. The pointer has been called the 
  648.  "go to" of data structures. It forces programmers to think of data as 
  649.  being bound to a fixed memory location where it can be manipulated in all 
  650.  sorts of low-level non-portable, tricky ways. A picture of the actual 
  651.  hardware that your program will run on is never far from your mind. Euphoria
  652.  does not have pointers and does not need them.
  653.  
  654.  
  655.  2.3 Declarations
  656.  ----------------
  657.  
  658.  Identifiers
  659.  -----------
  660.  Variable names and other user-defined symbols (identifiers) may be of any 
  661.  length. Upper and lower case are distinct. Identifiers must start with a 
  662.  letter and then be followed by letters, digits or underscores. The 
  663.  following reserved words have special meaning in Euphoria and may not be 
  664.  used as identifiers:
  665.  
  666.  and            end             include          to
  667.  by             exit            not              type
  668.  constant       for             or               while
  669.  do             function        procedure        with
  670.  else           global          return           without
  671.  elsif          if              then 
  672.  
  673.  The Euphoria editor displays these words in blue.
  674.  
  675.  The following kinds of user-defined symbols may be declared in a program:
  676.  
  677.      o  procedures 
  678.     These perform some computation and may have a list of parameters, 
  679.     e.g.
  680.    
  681.     procedure empty()
  682.     end procedure
  683.  
  684.     procedure plot(integer x, integer y)
  685.         position(x, y)
  686.         puts(1, '*')
  687.     end procedure
  688.     
  689.     There are a fixed number of named parameters, but this is not 
  690.     restrictive since any parameter could be a variable-length sequence 
  691.     of arbitrary objects. In many languages variable-length parameter 
  692.     lists are impossible.  In C, you must set up strange mechanisms that
  693.     are complex enough that the average programmer cannot do it without
  694.     consulting a manual or a local guru. 
  695.  
  696.     A copy of the value of each argument is passed in. The formal 
  697.     parameter variables may be modified inside the procedure but this does
  698.     not affect the value of the arguments.
  699.  
  700.     Performance Note: The interpreter does not actually copy sequences or
  701.     floating-point numbers unless it becomes necessary. For example,
  702.         
  703.         y = {1,2,3,4,5,6,7,8.5,"ABC"}
  704.         x = y
  705.     
  706.     The statement x = y does not actually cause a new copy of y to be 
  707.     created. Both x and y will simply "point" to the same sequence. If we 
  708.     later perform x[3] = 9, then a separate sequence will be created for x 
  709.     in memory (although there will still be just one shared copy of 8.5 and 
  710.     "ABC"). The same thing applies to "copies" of arguments passed in to
  711.     subroutines.
  712.     
  713.      o  functions 
  714.     These are just like procedures, but they return a value, and can be
  715.     used in an expression, e.g.
  716.  
  717.     function max(atom a, atom b)
  718.         if a >= b then
  719.         return a
  720.         else
  721.         return b
  722.         end if
  723.     end function
  724.  
  725.     Any Euphoria object can be returned.  You can, in effect, have 
  726.     multiple return values, by returning a sequence of objects. e.g.
  727.     
  728.     return {quotient, remainder}
  729.  
  730.     We will use the general term "subroutine", or simply "routine" when a
  731.     remark is applicable to both procedures and functions.
  732.  
  733.      o  types 
  734.     These are special functions that may be used in declaring the allowed
  735.     values for a variable. A type must have exactly one parameter and 
  736.     should return an atom that is either TRUE (non-zero) or FALSE (zero).
  737.     Types can also be called just like other functions. They are discussed
  738.     in more detail below.
  739.  
  740.      o  variables 
  741.     These may be assigned values during execution e.g.
  742.  
  743.     integer x
  744.     x = 25
  745.  
  746.     object a, b, c
  747.     a = {}
  748.     b = a
  749.     c = 0
  750.  
  751.      o  constants
  752.     These are variables that are assigned an initial value that can 
  753.     never change e.g.
  754.       
  755.     constant MAX = 100
  756.     constant Upper = MAX - 10, Lower = 5
  757.  
  758.     The result of any expression can be assigned to a constant,
  759.     even one involving calls to previously defined functions, but once
  760.     the assignment is made the value of the constant variable is 
  761.     "locked in".
  762.  
  763.  
  764.  Scope
  765.  -----
  766.  Every symbol must be declared before it is used. This is restrictive, but it
  767.  has benefits. It means you always know in which direction to look for the 
  768.  definition of a subroutine or variable that is used at some point in the 
  769.  program. When looking at a subroutine definition, you know that there could 
  770.  not be a call to this routine from any routine defined earlier. In general, 
  771.  it forces you to organize your program into a hierarchy where there are 
  772.  distinct, "layers" of low-level, followed by higher-level routines. You 
  773.  can replace a layer without disrupting any lower layers.
  774.  
  775.  A symbol is defined from the point where it is declared to the end of its 
  776.  scope. The scope of a variable declared inside a procedure or function (a 
  777.  private variable) ends at the end of the procedure or function.  The scope 
  778.  of all other constants, procedures, functions and variables ends at the end
  779.  of the source file in which they are declared and they are referred to as 
  780.  local, unless the word global precedes their declaration, in which case their 
  781.  scope extends indefinitely. Procedures and functions can call themselves 
  782.  recursively.
  783.  
  784.  Constant declarations must be outside of any subroutine.
  785.  
  786.  Variable declarations inside a subroutine must all appear at the beginning,
  787.  before the executable statements of the subroutine.
  788.  
  789.  A special case is that of the controlling variable used in a for-loop. It is 
  790.  automatically declared at the beginning of the loop, and its scope ends at 
  791.  the end of the for-loop. If the loop is inside a function or procedure, the 
  792.  loop variable is a private variable and may not have the same name as any 
  793.  other private variable. When the loop is at the top level, outside of any  
  794.  function or procedure, the loop variable is a local variable and may not have 
  795.  the same name as any other global or local variable in that file. You do not 
  796.  declare loop variables as you would other variables. The range of values 
  797.  specified in the for statement defines the legal values of the loop variable 
  798.  - specifying a type would be redundant and is not allowed.
  799.  
  800.  
  801.  Specifying the type of a variable
  802.  ---------------------------------
  803.  
  804.  Variable declarations have a type name followed by a list of the variables 
  805.  being declared. For example,
  806.  
  807.     object a 
  808.  
  809.     global integer x, y, z 
  810.  
  811.     procedure fred(sequence q, sequence r)
  812.  
  813.  In a parameter list like the one above, the type name may only be followed by 
  814.  a single variable name. 
  815.  
  816.  The types: object, sequence, atom and integer are predefined. Variables of 
  817.  type object may take on any value. Those declared with type sequence must  
  818.  always be sequences. Those declared with type atom must always be atoms. Those
  819.  declared with type integer must be atoms with integer values from -1073741824 
  820.  to +1073741823 inclusive. You can perform exact calculations on larger integer
  821.  values, up to about 15 decimal digits, but declare them as atom, rather than 
  822.  integer.
  823.  
  824.  Performance Note: Calculations using variables declared as integer will 
  825.  usually be somewhat faster than calculations involving variables declared as
  826.  atom. If your machine has floating-point hardware, Euphoria will use it to 
  827.  manipulate atoms that aren't representable as integers. If your machine 
  828.  doesn't have floating-point hardware, Euphoria will call software 
  829.  floating-point emulation routines contained in ex.exe. You can force 
  830.  Euphoria to bypass any floating-point hardware, by setting an environment 
  831.  variable:
  832.      
  833.      SET NO87=1
  834.  
  835.  The slower software routines will be used, but this could be of some 
  836.  advantage if you are worried about the floating-point bug in some Pentium 
  837.  chips.
  838.  
  839.  
  840.  To augment the predefined types, you can create new types. All you have to 
  841.  do is define a single-parameter function, but declare it with  
  842.  type ... end type instead of function ... end function. For example,
  843.  
  844.  
  845.     type hour(integer x)
  846.          return x >= 0 and x <= 23
  847.     end type
  848.  
  849.     hour h1, h2
  850.  
  851.  This guarantees that variables h1 and h2 can only be assigned integer values 
  852.  in the range 0 to 23 inclusive. After an assignment to h1 or h2 the 
  853.  interpreter will call "hour()", passing the new value.  The parameter x will 
  854.  first be checked to see if it is an integer. If it is, the return statement 
  855.  will be executed to test the value of x (i.e. the new value of h1 or h2). 
  856.  If "hour" returns true, execution continues normally. If "hour" returns false
  857.  then the program is aborted with a suitable diagnostic message. 
  858.  
  859.     procedure set_time(hour h)
  860.  
  861.  set_time() above can only be called with a reasonable value for parameter h.
  862.  
  863.  A variable's type will be checked after each assignment to the variable 
  864.  (except where the compiler can predetermine that a check will not be 
  865.  necessary), and the program will terminate immediately if the type function 
  866.  returns false.  Subroutine parameter types are checked when the subroutine 
  867.  is called. This checking guarantees that a variable can never have a value 
  868.  that does not belong to the type of that variable.
  869.  
  870.  Unlike other languages, the type of a variable does not affect any 
  871.  calculations on the variable. Only the value of the variable matters in an 
  872.  expression. The type just serves as an error check to prevent any "corruption"
  873.  of the variable. 
  874.  
  875.  Type checking can be turned off or on in between subroutines using the 
  876.  "with type_check" or "without type_check" commands. It is initially on by
  877.  default.
  878.  
  879.  Note to Benchmarkers: When comparing the speed of Euphoria programs against 
  880.  programs written in other languages, you should specify  without type_check 
  881.  at the top of the file, unless the other language provides a comparable 
  882.  amount of run-time checking. This gives Euphoria permission to skip runtime 
  883.  type checks, thereby saving some execution time. All other checks are still
  884.  performed, e.g. subscript checking, uninitialized variable checking etc. 
  885.  Even when you turn off type checking, Euphoria reserves the right to make 
  886.  checks at strategic places, since this can actually allow it to run your 
  887.  program faster in many cases. So you may still get a type check failure 
  888.  even when you have turned off type checking. With or without type_check, 
  889.  you will never get a machine-level exception. You will always get a 
  890.  meaningful message from Euphoria when something goes wrong.
  891.  
  892.  Euphoria's method of defining types is much simpler than what you will find 
  893.  in other languages, yet Euphoria provides the programmer with greater 
  894.  flexibility in defining the legal values for a type of data. Any algorithm 
  895.  can be used to include or exclude values. You can even declare a variable 
  896.  to be of type object which will allow it to take on any value. Routines can 
  897.  be written to work with very specific types, or very general types.
  898.  
  899.  Strict type definitions can greatly aid the process of debugging.  Logic 
  900.  errors are caught close to their source and are not allowed to propagate in 
  901.  subtle ways through the rest of the program. Furthermore, it is much easier 
  902.  to reason about the misbehavior of a section of code when you are guaranteed 
  903.  that the variables involved always had a legal value, if not the desired 
  904.  value.
  905.  
  906.  Types also provide meaningful, machine-checkable documentation about your 
  907.  program, making it easier for you or others to understand your code at a 
  908.  later date. Combined with the subscript checking, uninitialized variable 
  909.  checking, and other checking that is always present, strict run-time type 
  910.  checking makes debugging much easier in Euphoria than in most other 
  911.  languages. It also increases the reliability of the final program since 
  912.  many latent bugs that would have survived the testing phase in other 
  913.  languages will have been caught by Euphoria.
  914.  
  915.  Anecdote 1: In porting a large C program to Euphoria, a number
  916.  of latent bugs were discovered. Although this C program was believed to be 
  917.  totally "correct", we found: a situation where an uninitialized variable 
  918.  was being read; a place where element number "-1" of an array was routinely 
  919.  written and read; and a situation where something was written just off the 
  920.  screen. These problems resulted in errors that weren't easily visible to a 
  921.  casual observer, so they had survived testing of the C code. 
  922.  
  923.  Anecdote 2: The Quick Sort algorithm presented on page 117 of Writing 
  924.  Efficient Programs by Jon Bentley has a subscript error! The algorithm will 
  925.  sometimes read the element just before the beginning of the array to be 
  926.  sorted, and will sometimes read the element just after the end of the array. 
  927.  Whatever garbage is read, the algorithm will still work - this is probably 
  928.  why the bug was never caught. But what if there isn't any (virtual) memory 
  929.  just before or just after the array? Bentley later modifies the algorithm 
  930.  such that this bug goes away -- but he presented this version as being 
  931.  correct. Even the experts need subscript checking!
  932.  
  933.  Performance Note: When typical user-defined types are used extensively, type 
  934.  checking adds only 20 to 40 percent to execution time. Leave it on unless 
  935.  you really need the extra speed. You might also consider turning it off for
  936.  just a few heavily-executed routines. Profiling can help with this decision.
  937.  
  938.  
  939.  2.4 Statements
  940.  --------------
  941.  
  942.  The following kinds of executable statements are available:
  943.  
  944.     o   assignment statement
  945.  
  946.     o   procedure call 
  947.  
  948.     o   if statement
  949.  
  950.     o   while statement
  951.  
  952.     o   for statement 
  953.  
  954.     o   return statement
  955.  
  956.     o   exit statement
  957.  
  958.  Semicolons are not used in Euphoria, but you are free to put as many 
  959.  statements as you like on one line, or to split a single statement across 
  960.  many lines. You may not split a statement in the middle of a variable name, 
  961.  string, number or keyword. 
  962.  
  963.  An assignment statement assigns the value of an expression to a simple 
  964.  variable, or to a subscript or slice of a variable. e.g. 
  965.     
  966.     x = a + b
  967.  
  968.     y[i] = y[i] + 1
  969.  
  970.     y[i..j] = {1, 2, 3}
  971.  
  972.  The previous value of the variable, or element(s) of the subscripted or 
  973.  sliced variable are discarded.  For example, suppose x was a 1000-element 
  974.  sequence that we had initialized with:
  975.  
  976.     object x
  977.  
  978.     x = repeat(0, 1000)  -- repeat 0, 1000 times
  979.  
  980.  and then later we assigned an atom to x with:
  981.  
  982.     x = 7
  983.  
  984.  This is perfectly legal since x is declared as an object. The previous value 
  985.  of x, namely the 1000-element sequence, would simply disappear. Actually, 
  986.  the space consumed by the 1000-element sequence will be automatically 
  987.  recycled due to Euphoria's dynamic storage allocation.
  988.  
  989.  A procedure call starts execution of a procedure, passing it an optional list 
  990.  of argument values. e.g.
  991.  
  992.     plot(x, 23)
  993.  
  994.  An if statement tests an expression to see if it is 0 (false) or non-zero 
  995.  (true) and then executes the appropriate series of statements.  There may 
  996.  be optional elsif and else clauses. e.g.
  997.  
  998.     if a < b then
  999.         x = 1
  1000.     end if
  1001.  
  1002.  
  1003.     if a = 9 then
  1004.         x = 4
  1005.         y = 5
  1006.     else
  1007.         z = 8
  1008.     end if
  1009.  
  1010.  
  1011.     if char = 'a' then
  1012.         x = 1
  1013.     elsif char = 'b' then
  1014.         x = 2
  1015.     elsif char = 'c' then
  1016.         x = 3
  1017.     else
  1018.         x = -1
  1019.     end if
  1020.  
  1021.  A while statement tests an expression to see if it is non-zero (true), 
  1022.  and while it is true a loop is executed. e.g.
  1023.  
  1024.     while x > 0 do
  1025.         a = a * 2
  1026.         x = x - 1
  1027.     end while
  1028.  
  1029.  A for statement sets up a special loop with a controlling loop variable 
  1030.  that runs from an initial value up or down to some final value. e.g.
  1031.  
  1032.     for i = 1 to 10 do
  1033.         ? i   -- ? is a short form for print() -- see library.doc
  1034.     end for
  1035.  
  1036.     for i = 10 to 20 by 2 do
  1037.         for j = 20 to 10 by -2 do
  1038.         ? {i, j}
  1039.         end for
  1040.     end for
  1041.  
  1042.  The loop variable is declared automatically and exists until the end of the 
  1043.  loop. Outside of the loop the variable has no value and is not even declared.
  1044.  If you need its final value, copy it into another variable before leaving 
  1045.  the loop. The compiler will not allow any assignments to a loop variable. The
  1046.  initial value, loop limit and increment must all be atoms. If no increment 
  1047.  is specified then +1 is assumed. The limit and increment values are 
  1048.  established when the loop is entered, and are not affected by anything that 
  1049.  happens during the execution of the loop. 
  1050.  
  1051.  A return statement returns from a subroutine. If the subroutine is a function 
  1052.  or type then a value must also be returned. e.g.
  1053.  
  1054.     return
  1055.  
  1056.     return {50, "FRED", {}}
  1057.  
  1058.  An exit statement may appear inside a while-loop or a for-loop. It causes 
  1059.  immediate termination of the loop, with control passing to the first statement
  1060.  after the loop. e.g.
  1061.  
  1062.     for i = 1 to 100 do
  1063.         if a[i] = x then
  1064.         location = i
  1065.         exit
  1066.         end if
  1067.     end for
  1068.  
  1069.  It is also quite common to see something like this:
  1070.     
  1071.     constant TRUE = 1
  1072.     
  1073.     while TRUE do
  1074.          ...
  1075.          if some_condition then
  1076.             exit
  1077.          end if
  1078.          ...
  1079.     end while
  1080.  
  1081.  i.e. an "infinite" while-loop that actually terminates via an exit statement 
  1082.  at some arbitrary point in the body of the loop.
  1083.  
  1084.  
  1085.  2.5 Top-Level Commands
  1086.  ----------------------
  1087.  
  1088.  Euphoria processes your .ex file in one pass, starting at the first line and 
  1089.  proceeding through to the last line. When a procedure or function definition 
  1090.  is encountered, the routine is checked for syntax and converted into an 
  1091.  internal form, but no execution takes place. When a statement that is outside 
  1092.  of any routine is encountered, it is checked for syntax, converted into an 
  1093.  internal form and then immediately executed.  If your .ex file contains only 
  1094.  routine definitions, but no immediate execution statements, then nothing will 
  1095.  happen when you try to run it (other than syntax checking). You need to have 
  1096.  an immediate statement to call your main routine (see the example program in 
  1097.  section 1.1). It is quite possible to have a .ex file with nothing but 
  1098.  immediate statements, for example you might want to use Euphoria as a 
  1099.  desk calculator, typing in just one print (or ?) statement into a file, and 
  1100.  then executing it. The langwar demo program (euphoria\demo\langwar\lw.ex) 
  1101.  quickly reads in and displays a file on the screen, before the rest of the 
  1102.  program is compiled (on a 486 or higher this makes little difference as the 
  1103.  compiler takes less than a second to finish compiling the entire program). 
  1104.  Another common practice is to immediately initialize a global variable, just
  1105.  after its declaration.
  1106.  
  1107.  The following special commands may only appear at the top level i.e. 
  1108.  outside of any function or procedure. As we have seen, it is also 
  1109.  possible to use any Euphoria statement, including for-loops, while-loops, 
  1110.  if statements etc. (but not return), at the top level.
  1111.  
  1112.  include filename - reads in (compiles) a Euphoria source file in the presence 
  1113.            of any global symbols that have already been defined. 
  1114.            Global symbols defined in the included file remain visible
  1115.            in the remainder of the program. If an absolute pathname 
  1116.            is given, Euphoria will use it. When a relative pathname 
  1117.            is given, Euphoria will first look for filename in the 
  1118.            same directory as the main file given on the ex command 
  1119.            line. If it's not there, it will look in %EUDIR%\include, 
  1120.            where EUDIR is the environment variable that must be set 
  1121.            when using Euphoria. This directory contains the standard
  1122.            Euphoria include files.
  1123.            
  1124.            An include statement will be quietly ignored if the file has
  1125.            already been included, directly or indirectly.
  1126.  
  1127.  with            - turns on one of the compile options: profile, trace, 
  1128.            warning or type_check. Options warning and type_check are
  1129.            initially on, while profile and trace are initially off. 
  1130.  
  1131.  without         - turns off one of the above options. Note that each of 
  1132.            these options may be turned on or off between subroutines 
  1133.            but not inside of a subroutine. These options apply 
  1134.            globally. For example if you have:
  1135.  
  1136.             without type_check
  1137.             include graphics.e 
  1138.  
  1139.            then type checking will be turned off inside graphics.e as 
  1140.            well as in the current file. 
  1141.  
  1142.  
  1143.  Profiling         
  1144.  ---------
  1145.  If you specify "with profile" then an execution profile will be produced
  1146.  when your program finishes execution. It is written to the file "ex.pro" in
  1147.  the current directory.
  1148.  
  1149.  A profile is a listing of your program showing the number of times each 
  1150.  statement was executed. Only statements compiled "with profile" will be 
  1151.  shown. Normally you will say "with profile" at the top of your main .ex file,
  1152.  so you can get a complete listing. View this file with the Euphoria editor to
  1153.  see a color display.
  1154.  
  1155.  Profiling can help you in many ways: it lets you see which statements are 
  1156.  heavily executed, so you can try to speed up your program; it lets you verify
  1157.  that your program is actually working the way you intended; and it lets you
  1158.  see which sections of code were not tested - don't let your users be the 
  1159.  first!
  1160.    
  1161.  Redirecting Standard Input and Standard Output
  1162.  ----------------------------------------------
  1163.  Routines such as gets() and puts() can use standard input (file #0), 
  1164.  standard output (file #1), and standard error output (file #2). Standard 
  1165.  input and output can then be redirected as in:
  1166.  
  1167.     ex myprog < myinput  > myoutput
  1168.  
  1169.  See the I/O routines in Part II section 2.6 for more details.
  1170.  
  1171.  
  1172.  
  1173.             3. Debugging
  1174.             ============
  1175.  
  1176.  
  1177.  Debugging in Euphoria is much easier than in most other programming languages.
  1178.  The extensive runtime checking provided at all times by Euphoria automatically
  1179.  catches many bugs that in other languages might take hours of your time to 
  1180.  track down. When Euphoria catches an error, you will always get a brief 
  1181.  report on your screen, and a detailed report in a file called "ex.err". 
  1182.  These reports always include a full English description of what happened, 
  1183.  along with a call-stack traceback. The file ex.err will also have a dump of 
  1184.  all variable values, and optionally a list of the most recently executed 
  1185.  statements. For extremely large sequences, only a partial dump is shown.
  1186.  
  1187.  In addition, you are able to create user-defined types that precisely 
  1188.  determine the set of legal values for each of your variables. An error 
  1189.  report will occur the moment that one your variables is assigned an illegal 
  1190.  value.
  1191.  
  1192.  Sometimes a program will misbehave without failing any runtime checks. In 
  1193.  any programming language it may be a good idea to simply study the source 
  1194.  code and rethink the algorithm that you have coded. It may also be useful 
  1195.  to insert print statements at strategic locations in order to monitor the 
  1196.  internal logic of the program. This approach is particularly convenient in 
  1197.  an interpreted language like Euphoria since you can simply edit the source
  1198.  and rerun the program without waiting for a recompile/relink.  
  1199.  
  1200.  Euphoria provides you with additional powerful tools for debugging. You 
  1201.  can trace the execution of your program source code on one screen while 
  1202.  you witness the output of your program on another. with trace / without trace 
  1203.  commands select the subroutines in your program that are available for tracing.
  1204.  Often you will simply insert a "with trace" command at the very beginning of 
  1205.  your source code to make it all traceable. Sometimes it is better to place 
  1206.  the first "with trace" after all of your user-defined types, so you don't 
  1207.  trace into these routines after each assignment to a variable. At other times,
  1208.  you may know exactly which routine or routines you are interested in tracing, 
  1209.  and you will want to select only these ones. Of course, once you are in the 
  1210.  trace window you can interactively skip over the execution of any routine by 
  1211.  pressing down-arrow on the keyboard rather than Enter. 
  1212.  
  1213.  Only traceable lines can appear in ex.err as "most-recently-executed lines" 
  1214.  should a runtime error occur. If you want this information and didn't get it, 
  1215.  you should insert a "with trace" and then rerun your program. Execution will 
  1216.  be a bit slower when lines compiled "with trace" are executed.
  1217.  
  1218.  After you have predetermined the lines that are traceable, your program must 
  1219.  then dynamically cause the trace facility to be activated by executing a 
  1220.  trace(1) statement. Again, you could simply say:
  1221.  
  1222.     with trace
  1223.     trace(1)    -- or trace(2) if you prefer a mono display
  1224.  
  1225.  at the top of your program, so you can start tracing from the beginning of 
  1226.  execution. More commonly, you will want to trigger tracing when a certain 
  1227.  routine is entered, or when some condition arises. e.g.
  1228.     
  1229.     if x < 0 then
  1230.         trace(1)
  1231.     end if
  1232.  
  1233.  You can turn off tracing by executing a trace(0) statement. You can also 
  1234.  turn it off interactively by typing 'q' to quit tracing. Remember that 
  1235.  "with trace" must appear outside of any routine, whereas trace(1) and 
  1236.  trace(0) can appear inside a routine or outside.
  1237.  
  1238.  You might want to turn on tracing from within a type. Suppose you run 
  1239.  your program and it fails, with the ex.err file showing that one of your 
  1240.  variables has been set to a strange, although not illegal value, and you 
  1241.  wonder how it could have happened. Simply create a type for that variable 
  1242.  that executes trace(1) if the value being assigned to the variable is the 
  1243.  strange one that you are interested in.
  1244.  e.g.
  1245.     type positive_int(integer x)
  1246.         if x = 99 then
  1247.         trace(1) -- how can this be???
  1248.         return 1 -- keep going
  1249.         else
  1250.         return x > 0
  1251.         end if
  1252.     end type
  1253.  
  1254.  You will then be able to see the exact statement that caused your variable 
  1255.  to be set to the strange value, and you will be able to check the values 
  1256.  of other variables. You will also be able to check the output screen to 
  1257.  see what has been happening up to this precise moment. If you make your 
  1258.  special type return 0 for the strange value instead of 1, you can force a 
  1259.  dump into ex.err.
  1260.  
  1261.  The Trace Screen
  1262.  ----------------
  1263.  When a trace(1) statement is executed, your main output screen is saved and 
  1264.  a trace screen appears. It shows a view of your program with the statement 
  1265.  that will be executed next highlighted, and several statements before and 
  1266.  after showing as well. Several lines at the bottom of the screen are 
  1267.  reserved for displaying variable names and values. The top line shows the 
  1268.  commands that you can enter at this point:
  1269.  
  1270.  F1 - display main output screen - take a look at your program's output so far
  1271.  
  1272.  F2 - redisplay trace screen. Press this key while viewing the main output 
  1273.       screen to return to the trace display.
  1274.  
  1275.  Enter - execute the currently-highlighted statement only
  1276.  
  1277.  down-arrow - continue execution and break when any statement coming after 
  1278.           this one in the source listing is executed. This lets you skip 
  1279.           over subroutine calls. It also lets you force your way out of 
  1280.           repetitive loops.
  1281.  
  1282.  ? - display the value of a variable. Many variables are displayed 
  1283.      automatically as they are assigned a value, but sometimes you will have 
  1284.      to explicitly ask for one that is not on display. After hitting ?
  1285.      you will be prompted for the name of the variable. Variables that are 
  1286.      not defined at this point cannot be shown. Variables that have not yet 
  1287.      been initialized will have <NO VALUE> beside their name.
  1288.  
  1289.  q - quit tracing and resume normal execution. Tracing will start again when 
  1290.      the next trace(1) is executed.
  1291.  
  1292.  ! - this will abort execution of your program. A traceback and dump of 
  1293.      variable values will go to ex.err.
  1294.  
  1295.  As you trace your program, variable names and values appear automatically in 
  1296.  the bottom portion of the screen. Whenever a variable is assigned-to you will 
  1297.  see its name and new value appear at the bottom. This value is always kept 
  1298.  up-to-date. Private variables are automatically cleared from the screen 
  1299.  when their routine returns. When the variable display area is full, 
  1300.  least-recently referenced variables will be discarded to make room for 
  1301.  new variables. 
  1302.  
  1303.  For your convenience, numbers that are in the range of printable ASCII 
  1304.  characters (32-127) are displayed along with the ASCII character itself. The
  1305.  ASCII character will be in a different color (or in quotes in a mono display).
  1306.  This is done for all variables, since Euphoria does not know in general
  1307.  whether you are thinking of a number as an ASCII character or not. You will
  1308.  also see ASCII characters (in quotes) in ex.err. This can make for a rather
  1309.  "busy" display, but the ASCII information is often very useful.
  1310.  
  1311.  The trace screen adopts the same graphics mode as the main output screen. 
  1312.  This makes flipping between them quicker and easier.
  1313.  
  1314.  When a traced program requests keyboard input, the main output screen will 
  1315.  appear, to let you type your input as you normally would. This works fine for 
  1316.  gets() input. When get_key() (quickly samples the keyboard) is called you 
  1317.  will be given 10 seconds to type a character otherwise it is assumed that 
  1318.  there is no input for this call to get_key(). This allows you to test the 
  1319.  case of input and also the case of no input for get_key().
  1320.  
  1321.              --- END OF PART I ---
  1322.  
  1323.          see library.doc for Part II - Library Routines
  1324.          
  1325.  
  1326.